package com.leetcode.concurrency;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Dennis Li
 * @date 2020/7/20 16:04
 */
public class PrintInSequence_1114 {

    volatile CountDownLatch latch;

    private AtomicInteger signal;

    public PrintInSequence_1114() {
        signal = new AtomicInteger(3);
//        latch = new CountDownLatch(3);
    }

    public void first(Runnable printFirst) throws InterruptedException {
        printFirst.run();
        signal.decrementAndGet();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        while (signal.get() > 2) ;
        printSecond.run();
        signal.decrementAndGet();
    }

    public void third(Runnable printThird) throws InterruptedException {
        while (signal.get() > 1);
        printThird.run();
    }

}
